package com.xmtlxx.web.controller.sacw;

import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.utils.EncryptionProcessor;
import com.xmtlxx.wms.domain.ProBase;
import com.xmtlxx.wms.service.IInventoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
@Validated
@Api(value = "加密解密控制器", tags = {"加密解管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/sacw/encrypt")
public class EncryptController {

    private final IInventoryService iInventoryService;

    @ApiOperation("加密数据")
    @PostMapping("/encryptProBase")
    R<Integer> encryptProBase() {
        //获取PRO_BASE数据
        List<ProBase> proBases = iInventoryService.getProBaseList();
        ProBase[] proBases1 = proBases.toArray(new ProBase[0]);
        // 批量加密处理
        EncryptionProcessor.processBatch(proBases1);
        //存入数据库
        List<ProBase> savePros = new ArrayList<>();
        for (ProBase pro : proBases1) {
            savePros.add(pro);
        }
        iInventoryService.insertProBase(savePros);

        return R.ok();
    }

    public List<ProBase> decryptAesKeyByProBase(List<ProBase> list){
        ProBase[] proBases1 = list.toArray(new ProBase[0]);
        EncryptionProcessor.decryptProcessBatch(proBases1);
        List<ProBase> result = new ArrayList<>();
        for(ProBase pro : proBases1){
            result.add(pro);
        }
        return result;
    }


}
